// src/lib/Util.js
// 结束录音之后释放录音能力
export function afterAudioRecord() {
  getApp().globalData.audioRecording = false
}
// 结束音频播放之后释放音频播放能力
export function afterAudioPlay() {
  getApp().globalData.audioPlaying = false
}

/**
 * 判断是否可以录音或者播放
 * @param {string} type play | record
 */
export function beforeAudioRecordOrPlay(type) {
  const audioPlaying = getApp().globalData.audioPlaying
  const audioRecording = getApp().globalData.audioRecording
  if (audioPlaying ||audioRecording) {
    uni.showToast({
      title: audioPlaying ? '请先暂停其他音频播放' : '请先结束其他录音',
      icon: 'none'
    })
    return false
  } else {
    if (type === 'play') {
      getApp().globalData.audioPlaying = true
    } else if (type === 'record') {
      getApp().globalData.audioRecording = true
    } else {
      throw new Error('type Error', type)
    }
    return true
  }
}


//防抖函数
export function debounce(fn, delay = 100) {
  let timer = null;
  return function (...args) {
    if (timer !== undefined && timer !==null) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  }
}

// 对时间戳进行格式化处理
export function formatDate (date, fmt) {
  if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
  };
  for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + '';
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
      }
  }
  return fmt;
};

function padLeftZero (str) {
  return ('00' + str).substr(str.length);
};


export function dateDiff (timestamp) {
    // 补全为13位
    var arrTimestamp = (timestamp + '').split('');
    for (var start = 0; start < 13; start++) {
        if (!arrTimestamp[start]) {
            arrTimestamp[start] = '0';
        }
    }
    timestamp = arrTimestamp.join('') * 1;
 
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - timestamp;
 
    // 如果本地时间反而小于变量时间
    if (diffValue < 0) {
        return '不久前';
    }
 
    // 计算差异时间的量级
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
 
    // 数值补0方法
    var zero = function (value) {
        if (value < 10) {
            return '0' + value;
        }
        return value;
    };
 
    // 使用
    if (monthC > 12) {
        // 超过1年，直接显示年月日
        return (function () {
            var date = new Date(timestamp);
            return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
        })();
    } else if (monthC >= 1) {
        return parseInt(monthC) + "月前";
    } else if (weekC >= 1) {
        return parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
        return parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
        return parseInt(hourC) + "小时前";
    } else if (minC >= 1) {
        return parseInt(minC) + "分钟前";
    }
    return '刚刚';
};

var minute = 1000 * 60;
var hour = minute *60;
var day = hour *24;
var week = day * 7;
var month = day * 30;
export  function getTimer(stringTime){
    var time1 = new Date().getTime();//当前的时间戳
    console.log(time1);
    var time2 = Date.parse(new Date(stringTime));//指定时间的时间戳
    console.log(time2);
    var time = time1 - time2;

    var result = null;
    if(time < 0){
        alert("设置的时间不能早于当前时间！");
    }else if(time/month >= 1){
        result = "发布于" + parseInt(time/month) + "月前！";
    }else if(time/week >= 1){
        result = "发布于" + parseInt(time/week) + "周前！";
    }else if(time/day >= 1){
        result = "发布于" + parseInt(time/day) + "天前！";
    }else if(time/hour >= 1){
        result = "发布于" + parseInt(time/hour) + "小时前！";
    }else if(time/minute >= 1){
        result = "发布于" + parseInt(time/minute) + "分钟前！";
    }else {
        result = "刚刚发布！";
    }
    console.log(result);
}